﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Data.ProjectModule;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.Apps.ServiceImpl.ProjectModule
{
    public partial class ProjectService
    {
        public StatusInfo<DailyWorks> GetDailyWorks(Guid userId, DateTime day)
        {
            return StatusInfo.Wrap(
                () =>
                {
                    using (var container = new EzDeskProjectContainer())
                    {
                        container.ContextOptions.ProxyCreationEnabled = false;
                        var dailyWork = container.DailyWorksSet.FirstOrDefault(dw => dw.Day.Value == day.Date && dw.UserId == userId);
                        if (dailyWork == null)
                        {
                            dailyWork = new DailyWorks {Day = day, DailyWork = "", UserId = userId};
                        }

                        return dailyWork;
                    }
                }
                );
        }

        public StatusInfo<DailyWorks> UpdateDailyWorks(Guid userId, DateTime day, string content)
        {
            return StatusInfo.Wrap(
                () =>
                {
                    using (var container = new EzDeskProjectContainer())
                    {
                        container.ContextOptions.ProxyCreationEnabled = false;
                        var dailyWork = container.DailyWorksSet.FirstOrDefault(dw => dw.Day.Value == day.Date && dw.UserId == userId);
                        if (dailyWork == null)
                        {
                            dailyWork = new DailyWorks();
                            dailyWork.Id = Guid.NewGuid();
                            dailyWork.UserId = userId;
                            dailyWork.Day = day.Date;

                            container.DailyWorksSet.AddObject(dailyWork);
                        }
                        dailyWork.DailyWork = content;
                        container.SaveChanges();

                        return dailyWork;
                    }
                }
                );
        }

    }
}
